<?php

namespace app\controller;

use app\help\PaginationHelper;
use app\validate\UserValidate;
use Ramsey\Uuid\Uuid;
use think\annotation\route\Group;
use think\annotation\route\Route;
use app\BaseController;
use app\middleware\CheckToken;
use think\exception\ValidateException;
use think\facade\Db;
use app\model\User;
use think\facade\Request;


#[Group("api")]
class UserController extends BaseController
{
    protected $middleware = [CheckToken::class];

    #[Route("POST", "queryUser")]
    public function queryUserList()
    {
        $query = Db::name('sys_user')->order("create_time desc");
        if (!empty(Request::param('username'))) {
            $query->where('name', 'like', '%'. Request::param('username') . '%');
        }
        if (!empty(Request::param('password'))) {
            $query->where('password', 'like', '%'. Request::param('password') . '%');
        }
        if (!empty(Request::param('pageNo')) && !empty(Request::param('pageSize'))) {
            $pageParams = PaginationHelper::calculatePagination(Request::param());
            $user_list=$query->paginate([
                'list_rows' => Request::param('pageSize'),
                'page' =>Request::param('pageNo'),
            ]);
        }else{
            $user_list = $query->select();
        }
        return json($user_list,200);
    }


    #[Route("POST", "updateUserStatus")]
    public function updateUserStatus(){
         $status = Request::param('status');
         if(empty(Request::param('id')  ) || !isset($status)){
             return json(['msg' => '参数错误'],500);
         }
         $user = User::find(Request::param('id'));
         if(empty($user)){
             return json(['msg' => '用户不存在'],500);
         }
         $user->status = Request::param('status');
         $user->save();
         return json(['user'=> $user]);
    }



    #[Route("POST", "addUser")]
    public function addUser(){
        try{
            validate(UserValidate::class)->check(request()->post());
        }catch (ValidateException $e){
            return json(['msg' => $e->getError()],500);
        }
        $user = new User(Request::post());
        $user->id = Uuid::uuid4();
      $user->save();
        return json(['msg'=>'新增用户'],200);
    }

    #[Route("POST", "modifyUser")]
    public function modifyUser(){
        try{
            validate(UserValidate::class)->scene('modify')->check(request()->post());
        }catch (ValidateException $e){
            return json(['msg' => $e->getError()],500);
        }

        $user = User::find(Request::param('id'));
        if(empty($user)){
            return json(['msg' => '用户不存在'],500);
        }else{
            $user->update(Request::post());
            return json(['msg'=>'修改用户成功']);

        }
    }


    #[Route("POST", "deleteUser")]
    public function deleteUser()
    {
        $id = request()->post('id');
        if(empty($id)){
            return json(['msg' => `id不能为空`], 500);
        }
        $user = User::find($id);
        if (empty($user)) {
            return json(['msg' => '用户不存在'], 500);
        } else {
            $result = $user->delete();
        }
        return json($result);
    }
}